﻿using Colorful.Furniture.PermissionManagement;
using FreeSql;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Colorful.Furniture.Enum;
using Colorful.Furniture.ErpInventoryManagement;
using CoreHtmlToImage;
using Volo.Abp.Application.Services;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Users;
using Colorful.Furniture.ErpOrderManagement;
using Colorful.Furniture.ErpSupplierManagement;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.ProductOrderBatchGetAftersaleOrderResponse.Types;
using Colorful.Furniture.MpManagement;
using Microsoft.Extensions.Logging;
using Colorful.Furniture.FinanceSdk;
using Volo.Abp;

namespace Colorful.Furniture.ErpCustomerManagement;

/// <summary>
/// 凭证管理服务
/// </summary>
public class VoucherAppService : ApplicationService
{
    private readonly IFreeSql _freeSql;
    private readonly IObjectMapper _objectMapper;
    private readonly ILogger<WechatMpJsSdkAppService> _logger;

    public VoucherAppService(
        IFreeSql freeSql,
        IObjectMapper objectMapper,
        ILogger<WechatMpJsSdkAppService> logger
    )
    {
        _freeSql = freeSql;
        _objectMapper = objectMapper;
        _logger = logger;
    }


    /// <summary>
    /// 生成QQ凭证
    /// </summary>
    /// <param name="orderDownNo">订单号（平台生成）或合作商唯一订单号</param>
    /// <returns></returns>
    public async Task<string> GetMakeImageQqAsync(string orderDownNo)
    {
        ////获得订单信息
        //var orderInfo = await _freeSql.Select<CustomerOrder>()
        //    .Where(x => x.OrderDownNo == orderDownNo || x.OutTradeNo == orderDownNo)
        //    .DisableGlobalFilter("SoftDelete")
        //    .ToOneAsync();
        //if (orderInfo != null)
        //{
        //    var list = await _freeSql.Select<CustomerOrder, InventoryProduct, CustomerOrderCallbackLog>()
        //        .LeftJoin((x,y,z) => x.ProductNo == y.ProductNo && x.SupplierId == y.SupplierId && x.SupplierProductNo == y.SupplierProductNo)
        //        .LeftJoin((x, y, z) => x.Id == z.OrderId && z.Type == 1 )
        //        .Where((x, y, z) => x.RechargeAccount == orderInfo.RechargeAccount && x.Status == EnumOrderStatus.订单完成)
        //        .Where((x, y, z) => x.AddTime.Date == orderInfo.AddTime.Date)
        //        .OrderByDescending((x, y, z) => x.AddTime)
        //        .ToListAsync((x, y, z) => new
        //        {
        //            x.AddTime,
        //            x.CustomerQty,
        //            y.SupplierQty,
        //            RetTime = z.OrderDownNo == null ? x.AddTime : z.AddTime
        //        });

        //    StringBuilder rechargeList = new StringBuilder();
        //    foreach (var item in list)
        //    {
        //        rechargeList.Append(@$" 
        //        <tr>
        //          <td class=""style-grey"">{item.RetTime.ToString("yyyy-MM-dd HH:mm:ss")}</td>
        //          <td style=""color:#333;"">Q币</td>
        //          <td style=""color:#333;"">{item.CustomerQty * item.SupplierQty}</td>
        //          <td style=""color:#333;"">在线充值</td>
        //          <td style=""color:#333;"">{orderInfo.RechargeAccount} Q币充值</td>
        //        </tr>
        //        ");
        //    }
        //    Thread.Sleep(100);
        //    var timeNow = orderInfo.AddTime.ToString("yyyyMMdd");

        //    var html = @$" <!DOCTYPE html PUBLIC ""-//W3C//DTD XHTL 1.0 Transitional//EN"" ""https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
        //    <html xmlns=""https://www.w3.org/1999/xhtml"">
        //    <head>
        //    <meta http-equiv=""X-UA-Compatible"" content=""IE=EmulateIE7"" />
        //    <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />
        //    <base href=""qb.qq.com"" />
        //    <title>腾讯渠道合作伙伴门户</title>
        //    <link href=""http://www.jiangxhj.com/pinzheng/qq/css/common.css"" rel=""stylesheet"" type=""text/css"" />
        //    <link href=""http://www.jiangxhj.com/pinzheng/qq/css/logined.css"" rel=""stylesheet"" type=""text/css"" />
        //    <link href=""http://www.jiangxhj.com/pinzheng/qq/css/thickbox_new.css"" rel=""stylesheet"" type=""text/css"" />
        //    <style type=""text/css"" media=""all"">
        //    @import ""http://www.jiangxhj.com/pinzheng/qq/css/thickbox3.1.css"";
        //    </style>
        //    </head>
        //    <body >
        //    <!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
        //    <html xmlns=""https://www.w3.org/1999/xhtml"">
        //    <head>
        //    <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />
        //    <title></title>
        //    <link href=""http://www.jiangxhj.com/pinzheng/qq/css/common.css"" rel=""stylesheet"" type=""text/css"" />
        //    </head>
        //    <body>
        //    <div id=""top"">
        //      <div id=""toplogo""><a href=""https://qb.qq.com""><img src=""https://qb.qq.com/images/top_logo.gif"" alt=""腾讯渠道合作伙伴门户"" height=""44"" /></a></div>  
        //      <div id=""toplink""><a href='https://qb.qq.com/cgi-bin/qbas_acct_manage_baseinfo.cgi#' target='ifrpage'>查看个人信息详情</a> | <a href=""/help/help.shtml"">帮助中心</a> | <a href=""//tgs.qq.com/"" target='_blank' >TGS登录</a> | <a href=""//qb.qq.com/qbas/index.php/account/logout/index"" class=""a-orange-line "">退出登录</a></div>
        //      <div id=""logininfo"" style="" float:right;text-align:right; margin-right:-216px; margin-top:44px; padding:4px 4px 3px 60px;background:#d0ebf9; font-family:Verdana""> &nbsp;&nbsp;&nbsp;&nbsp;2020年下半年度总积分：<span class='style-orange'>0</span> &nbsp;&nbsp; &nbsp;&nbsp;<a href='https://qb.qq.com/qbas/index.php/qbquery/qbquery_jump/get_session_key/9' target='ifrpage'>详情</a></div>  
        //    </div>
        //    </body>
        //    </html>
        //    <div id=""loginedtop""></div>   
        //    <div id=""main"">
        //      <!--菜单-->  
        //    <div class=""borderout"" id=""menu"">
        //      <div class=""borderin"">
        //        <dl>
        //          <dt>消息与客户支持</dt>      
        //          <dd><a id=""menu_11"" href=""cgi-bin/online/qbas_message_long"" target=""ifrpage"" onfocus=""this.blur();"">查看我的消息</a></dd>      
        //          <dd><a id=""menu_12"" href=""/cgi-bin/qbas_appeal_query.cgi#"" target=""ifrpage"" onfocus=""this.blur();"">反馈查询</a></dd>      
        //          <dd><a id=""menu_13"" href=""https://qb.qq.com/cgi-bin/qbas_zhixiaoshang_customer_service.cgi"" target=""ifrpage"" onfocus=""this.blur();"">提交在线支持</a></dd>
        //          <dt>在线卡销售管理</dt>      
        //          <dd><a id=""menu_219"" href=""/cgi-bin/qbas_business_request_show.cgi"" target=""ifrpage"" onfocus=""this.blur();"">我要进货</a></dd>      
        //          <dd><a id=""menu_220"" href=""https://qb.qq.com/esales_sales_selectpro.shtml"" target=""ifrpage"" onfocus=""this.blur();"">我要销售</a></dd>      
        //          <dd><a id=""menu_221"" href=""https://qb.qq.com/cgi-bin/qbas_tran_sell_display.cgi"" target=""ifrpage"" onfocus=""this.blur();"" class=""menustyle02"">交易查询</a></dd>      
        //          <dd><a id=""menu_222"" href=""https://qb.qq.com/cgi-bin/qbas_trade_apply.cgi?s_type=1"" target=""ifrpage"" onfocus=""this.blur();"">已收到的转帐申请</a></dd>      
        //          <dd><a id=""menu_223"" href=""https://qb.qq.com/cgi-bin/qbas_trade_apply2.cgi?s_type=1"" target=""ifrpage"" onfocus=""this.blur();"">进货查询</a></dd>      
        //          <dd><a id=""menu_224"" href=""https://qb.qq.com/cgi-bin/esales_quota_show.cgi"" target=""ifrpage"" onfocus=""this.blur();"">交易金额上限管理</a></dd>
        //          <dt>账户管理</dt>      
        //          <dd><a id=""menu_340"" href="" https://qb.qq.com/manage_ztc2.shtml"" target=""ifrpage"" onfocus=""this.blur();"">直通车管理</a></dd>      
        //          <dd><a id=""menu_341"" href=""https://qb.qq.com/cgi-bin/qbas_acct_manage_baseinfo.cgi#"" target=""ifrpage"" onfocus=""this.blur();"">基本信息管理</a></dd>      
        //          <dd><a id=""menu_342"" href=""/cgi-bin/qbas_acct_show_change_passwd.cgi"" target=""ifrpage"" onfocus=""this.blur();"">密码管理</a></dd>      
        //          <dd><a id=""menu_343"" href=""/manage_mbk.shtml#"" target=""ifrpage"" onfocus=""this.blur();"">密保管理</a></dd>      
        //          <dd><a id=""menu_344"" href=""/cgi-bin/qbas_jinhuoren_search_show.cgi"" target=""ifrpage"" onfocus=""this.blur();"">指定归属</a></dd>
        //          <dt>推广员管理</dt>      
        //          <dd><a id=""menu_460"" href="" https://qb.qq.com/promoter_sys/index.php/promoter_admin/manager/index"" target=""ifrpage"" onfocus=""this.blur();"">推广员管理列表<img src=""https://qbquery.qq.com/qbas/images/rank_icon.gif"" /></a></dd>
        //        </dl>
        //      </div>
        //    </div>
        //      <div id=""content"">
        //        <div id=""mainifr"">
        //          <html xmlns=""https://www.w3.org/1999/xhtml""><head>
        //            <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"">
        //            <title></title>
        //            <link href=""http://www.jiangxhj.com/pinzheng/qq/ns/newsys.css"" rel=""stylesheet"" type=""text/css"">        
        //            </head>
        //            <body >
        //            <!--销售报表-->
        //            <div id=""menu1""><img src=""https://qb.qq.com/images/tran_query_nav_1.png"" alt=""菜单"" border=""0"" usemap=""#Map"">
        //            </div>
        //            <div class=""borderout"" id=""ordermanage"">
        //              <div class=""borderin"">
        //                <div class=""title"">
        //                  <h3 class=""titletext"">销售记录查询</h3>
        //                </div>
        //                <form name=""query_form"" >
        //                <table>
        //                  <thead>                     
        //                    <tr>
        //                      <td colspan=""5""><div class=""dateform"">&nbsp;&nbsp;开始时间：
        //                          <input name=""start_YMD"" type=""text"" class=""inputtext"" id=""start_YMD"" value=""{timeNow}"" size=""8"" maxlength=""8"">
        //                          &nbsp;&nbsp;结束时间：
        //                          <input name=""end_YMD"" type=""text"" class=""inputtext"" id=""end_YMD"" value=""{timeNow}"" size=""8"" maxlength=""8"">
        //                          &nbsp;&nbsp;交易类型：
        //                          <select name=""tran_type"" id=""tran_type"">
        //                               <option value="""" selected="""">--请选择--</option>
        //                               <option value=""0"">在线充值</option>
        //                               <option value=""1"">短信充值</option>				      	 
        //                           </select> 
        //                          &nbsp;&nbsp;对方帐户：
        //                          <input id=""tran_acct_name"" name=""tran_acct_name"" type=""text"" class=""inputtext"" value=""{orderInfo.RechargeAccount}"">          
        //                          <label>                      
        //                          &nbsp;&nbsp;
        //                          <input name=""button"" type=""button"" class=""input-button"" id=""button"" value=""查询"" onclick=""checkAndSumbit()"">
        //                          </label>
        //                        </div></td>
        //                    </tr>                
        //                  <tr>
        //                    <th>交易日期</th>
        //                    <th>商品类别</th>
        //                    <th>交易数量</th>
        //                    <th>交易方式</th>
        //                    <th>对方帐户</th>
        //                  </tr>
        //                  </thead>              
        //                  <tbody>                
        //                    {rechargeList}
        //                  </tbody>
        //                  <tfoot>              
        //                    <tr>
        //                        <td colspan=""11"" class=""style-grey right"">                    
        //                            <input type=""hidden"" value=""20"" id=""max_num"" name=""max_num"">
        //                          <input type=""hidden"" value=""1"" id=""page_no"" name=""page_no"">
        //                            <input type=""hidden"" value=""161"" id=""max_page_num"" name=""max_page_num"">
        //                          <input type=""hidden"" value=""3213"" id=""total_num"" name=""total_num"">                        
        //                           共{list.Count}条&nbsp;&nbsp;第<font id=""f_page_no"">1</font>/<font id=""f_max_page_num"">1</font>页
        //                           <a href=""#"" onclick=""prepage();return false ;"">上一页</a>&nbsp;&nbsp; <a href=""#"" onclick=""nextpage();return false ;"">下一页</a>&nbsp;&nbsp; 
        //                            转到                       
        //                             <select name=""select_page_no"" id=""select_page_no"" onchange=""selectChange();"">
        //                             <option value=""1"">1</option><option value=""2"">2</option><option value=""3"">3</option><option value=""4"">4</option><option value=""5"">5</option><option value=""6"">6</option><option value=""7"">7</option><option value=""8"">8</option><option value=""9"">9</option><option value=""10"">10</option><option value=""11"">11</option><option value=""12"">12</option><option value=""13"">13</option><option value=""14"">14</option><option value=""15"">15</option><option value=""16"">16</option><option value=""17"">17</option><option value=""18"">18</option><option value=""19"">19</option><option value=""20"">20</option><option value=""21"">21</option><option value=""22"">22</option><option value=""23"">23</option><option value=""24"">24</option><option value=""25"">25</option><option value=""26"">26</option><option value=""27"">27</option><option value=""28"">28</option><option value=""29"">29</option><option value=""30"">30</option><option value=""31"">31</option><option value=""32"">32</option><option value=""33"">33</option><option value=""34"">34</option><option value=""35"">35</option><option value=""36"">36</option><option value=""37"">37</option><option value=""38"">38</option><option value=""39"">39</option><option value=""40"">40</option><option value=""41"">41</option><option value=""42"">42</option><option value=""43"">43</option><option value=""44"">44</option><option value=""45"">45</option><option value=""46"">46</option><option value=""47"">47</option><option value=""48"">48</option><option value=""49"">49</option><option value=""50"">50</option><option value=""51"">51</option><option value=""52"">52</option><option value=""53"">53</option><option value=""54"">54</option><option value=""55"">55</option><option value=""56"">56</option><option value=""57"">57</option><option value=""58"">58</option><option value=""59"">59</option><option value=""60"">60</option><option value=""61"">61</option><option value=""62"">62</option><option value=""63"">63</option><option value=""64"">64</option><option value=""65"">65</option><option value=""66"">66</option><option value=""67"">67</option><option value=""68"">68</option><option value=""69"">69</option><option value=""70"">70</option><option value=""71"">71</option><option value=""72"">72</option><option value=""73"">73</option><option value=""74"">74</option><option value=""75"">75</option><option value=""76"">76</option><option value=""77"">77</option><option value=""78"">78</option><option value=""79"">79</option><option value=""80"">80</option><option value=""81"">81</option><option value=""82"">82</option><option value=""83"">83</option><option value=""84"">84</option><option value=""85"">85</option><option value=""86"">86</option><option value=""87"">87</option><option value=""88"">88</option><option value=""89"">89</option><option value=""90"">90</option><option value=""91"">91</option><option value=""92"">92</option><option value=""93"">93</option><option value=""94"">94</option><option value=""95"">95</option><option value=""96"">96</option><option value=""97"">97</option><option value=""98"">98</option><option value=""99"">99</option><option value=""100"">100</option><option value=""101"">101</option><option value=""102"">102</option><option value=""103"">103</option><option value=""104"">104</option><option value=""105"">105</option><option value=""106"">106</option><option value=""107"">107</option><option value=""108"">108</option><option value=""109"">109</option><option value=""110"">110</option><option value=""111"">111</option><option value=""112"">112</option><option value=""113"">113</option><option value=""114"">114</option><option value=""115"">115</option><option value=""116"">116</option><option value=""117"">117</option><option value=""118"">118</option><option value=""119"">119</option><option value=""120"">120</option><option value=""121"">121</option><option value=""122"">122</option><option value=""123"">123</option><option value=""124"">124</option><option value=""125"">125</option><option value=""126"">126</option><option value=""127"">127</option><option value=""128"">128</option><option value=""129"">129</option><option value=""130"">130</option><option value=""131"">131</option><option value=""132"">132</option><option value=""133"">133</option><option value=""134"">134</option><option value=""135"">135</option><option value=""136"">136</option><option value=""137"">137</option><option value=""138"">138</option><option value=""139"">139</option><option value=""140"">140</option><option value=""141"">141</option><option value=""142"">142</option><option value=""143"">143</option><option value=""144"">144</option><option value=""145"">145</option><option value=""146"">146</option><option value=""147"">147</option><option value=""148"">148</option><option value=""149"">149</option><option value=""150"">150</option><option value=""151"">151</option><option value=""152"">152</option><option value=""153"">153</option><option value=""154"">154</option><option value=""155"">155</option><option value=""156"">156</option><option value=""157"">157</option><option value=""158"">158</option><option value=""159"">159</option><option value=""160"">160</option><option value=""161"">161</option></select>                                      
        //                             页                    
        //                         </td>
        //                      </tr>                
        //                  </tfoot>
        //                </table>
        //                </form>
        //              </div>
        //              <div class=""btndiv""><img style=""cursor:pointer"" onclick=""send_request('https://qb.qq.com/cgi-bin/qbas_tran_query_latest.cgi')"" src=""https://qb.qq.com/images/btn_downsales.gif"" alt=""下载"" border=""0"">　<a href=""/cgi-bin/qbas_tran_sell_display.cgi""><img src=""https://qb.qq.com/images/btn_back.gif"" alt=""返回"" border=""0""></a></div>
        //            </div>
        //            <!--您知道吗-->
        //            <div class=""borderout"" id=""tips_orderstep"">
        //              <div class=""borderin"">
        //                <div class=""title"">
        //                  <h3 class=""titletext"">您知道吗？</h3>
        //                </div>
        //                <p> 该部分为您提供QQ在线卡销售报表查询功能，您可根据您的需要设定查询条件，查询不同类型的销售报表。</p>
        //              </div>
        //            </div>        
        //            </body>
        //          </html>
        //        </div>
        //      </div>
        //    </div>
        //    </body>
        //    </html>
        //";

        //    Thread.Sleep(100);
        //    var converter = new HtmlConverter();
        //    var bytes = converter.FromHtmlString(html);
        //    File.WriteAllBytes(@$"D:\wwwroot\jxhj\Api\wwwroot\pinzheng\qq\images\{orderInfo.AddTime.ToString("yyyyMM")}\{orderDownNo}.jpg", bytes); //服务器地址

        //    //File.WriteAllBytes(@$"E:\项目\jxhj\API\jxhj_new\test\src\Colorful.Furniture.HttpApi.Host\wwwroot\pinzheng\qq\images\{orderInfo.AddTime.ToString("yyyyMM")}\{orderDownNo}.jpg", bytes); //本地测试地址

        //    _logger.LogWarning($"【成功】生成订单凭证信息，订单号：{orderDownNo}");

        //    return "true";
        //}
        //else
        //{
        //    return "订单不存在，凭证生成失败";
        //}

        return "";




    }
}

